import {Process, Processor} from "@nestjs/bull";
import {type Job} from "bull";
import {MailerService} from "@nestjs-modules/mailer";
import {SendEmailRequest} from '@libs/common/protos/email/email';


@Processor('smtp')
export class MicroEmailProcessor {
    constructor(private readonly mailerService: MailerService) {
        console.log('MailerService 已注入：', !!mailerService);
    }

    @Process('welcome')
    async sendWelcomeEmail(job: Job<{ data: SendEmailRequest }>) {
        console.log('开始处理 welcome 队列任务，数据：', job.data);
        const sendEmailData = job.data.data;
        try {
            console.log('收件人邮箱：', sendEmailData.to,sendEmailData.from);
            await this.mailerService.sendMail(
                {
                    to: sendEmailData.to,
                    subject: sendEmailData.subject,
                    template:'welcome',
                    context:{
                        name:sendEmailData.metadata?.name || 'User',
                        currentYear: new Date().getFullYear(),
                    }
                }
            );
            console.log('邮件发送成功');
        } catch (error) {
            console.error('邮件发送失败：', error.message);
        }

    }

    @Process('reset-password')
    async sendResetPasswordEmail(job: Job<SendEmailRequest>) {
        const data = job.data;
        await this.mailerService.sendMail(data);
    }
}
